package com.sat.learning;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sat.learning.Views.RefreshView;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import static android.os.Environment.getDataDirectory;

public class MainActivity extends AppCompatActivity {

    private RefreshView lv_refresh;
    private ArrayList<String> listDatas;
    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
        initData();
    }

    private void initData() {
        listDatas = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            listDatas.add("这是一条ListView数据：" + i);
        }
        mAdapter = new MyAdapter();
        lv_refresh.setAdapter(mAdapter);
    }

    private void initUI() {
        lv_refresh = (RefreshView) findViewById(R.id.lv_refresh);
        lv_refresh.setOnRefreshListener(new RefreshView.onRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SystemClock.sleep(1500);
                        listDatas.add(0,"我是刷新出来的数据");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mAdapter.notifyDataSetChanged();
                                lv_refresh.setRefreshComplete();
                            }
                        });
                    }
                }).start();
            }

            @Override
            public void onMoreRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SystemClock.sleep(1500);
                        listDatas.add("我是下拉加载的数据");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mAdapter.notifyDataSetChanged();
                                lv_refresh.setRefreshComplete();
                            }
                        });
                    }
                }).start();
            }
        });
    }

    public void next(View view) {
        Intent intent = new Intent(this, NewsActivity.class);
        startActivity(intent);
    }

    public void avgle(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                getAvgle();
            }
        }).start();
    }

    private void getAvgle() {
        try {
            Log.d("TAG","进入网络");
            URL url = new URL("https://api.avgle.com/v1/categories");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //connection.setRequestMethod("GET");
            connection.setConnectTimeout(2000);
            int code = connection.getResponseCode();
            Log.d("TAG","请求码"+code);
            if (code == 200){
                InputStream inputStream = connection.getInputStream();
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                String path = getApplicationContext().getFilesDir().getAbsoluteFile() + File.separator + "json.txt";
                String path_sd = Environment.getExternalStorageDirectory().getAbsoluteFile() + File.separator + "json.txt";

                Log.d("TAG","内存:"+path+"===SDCard:"+path_sd);
                File file = new File(path);
                File file_sd = new File(path_sd);
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                FileOutputStream fileOutputStream_sd = new FileOutputStream(file_sd);
                byte[] buffer = new byte[1024];
                int temp = -1;
                while ((temp=inputStream.read(buffer))!=-1){
                    outputStream.write(buffer,0,temp);
                    fileOutputStream.write(buffer,0,temp);
                    fileOutputStream_sd.write(buffer,0,temp);
                }
                String json = outputStream.toString();
                //Log.d("TAG","请求成功"+json);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return listDatas.size();
        }

        @Override
        public String getItem(int position) {
            return listDatas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView = new TextView(parent.getContext());
            textView.setTextSize(16f);
            textView.setText(listDatas.get(position));
            return textView;
        }
    }
}
